/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is.ru.foodplanner;

import is.ru.domain.Course;
import is.ru.domain.Ingredient;
import is.ru.domain.TestData;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JSlider;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 *
 * @author davidhalldor
 */
public class FoodplannerUI extends javax.swing.JFrame implements TableModelListener{

    /**
     * Creates new form FoodplannerUI
     */
    
    private List<Course> appetizers;
    private List<Course> mainCourses;
    private List<Course> desserts;
    TestData dataModel;
    boolean userClickedCmb4 = false;
    
    public FoodplannerUI() {
        initComponents();
        
        jTable1.getModel().addTableModelListener(this);
        
        dataModel = new TestData();      
        dataModel.generateTestData();
        desserts = dataModel.getDesserts();
        mainCourses = dataModel.getMainCourses();
        appetizers = dataModel.getAppetizers();

        for (Course appetizer : appetizers) {
            jComboBox1.addItem(appetizer.getName());
        }
        for (Course mainCourse : mainCourses) {
            jComboBox2.addItem(mainCourse.getName());
        }
    
        for (Course desert : desserts) {
            jComboBox3.addItem(desert.getName());
        }
        jTable1.removeColumn(jTable1.getColumnModel().getColumn(4));
        ((JComponent) jTable1.getDefaultRenderer(Boolean.class)).setOpaque(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Food Planner");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jLabel2.setText("Persons");

        jSlider1.setMaximum(8);
        jSlider1.setMinimum(1);
        jSlider1.setMinorTickSpacing(1);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setValue(1);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jLabel3.setText("Appetizers");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select..." }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel4.setText("Main Courses");

        jLabel5.setText("Deserts");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select..." }));
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select..." }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel7.setText("1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ingredients", "Amount", "Purchased", "Cost", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTable1PropertyChange(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);

        jButton3.setText("Clear");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select..." }));
        jComboBox4.setEnabled(false);
        jComboBox4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox4MouseClicked(evt);
            }
        });
        jComboBox4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox4ItemStateChanged(evt);
            }
        });
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });

        jLabel8.setText("Side Course");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jSlider1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jPanel2Layout.createSequentialGroup()
                                    .add(jLabel2)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jLabel7))
                                .add(jLabel3)
                                .add(jLabel4)
                                .add(jComboBox1, 0, 151, Short.MAX_VALUE)
                                .add(jComboBox2, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jLabel5)
                                .add(jComboBox3, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(jButton3)
                            .add(jComboBox4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(6, 6, 6)
                                .add(jLabel8)))
                        .add(0, 0, Short.MAX_VALUE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel7))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSlider1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(4, 4, 4)
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jComboBox4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jComboBox3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton3)
                .addContainerGap())
            .add(jPanel2Layout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 352, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
        );

        jButton1.setText("Print Ingredients");

        jButton2.setText("Get Instructions");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText("Total Cost: ");

        jTextField1.setText("0");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .add(24, 24, 24)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton2)
                    .add(jLabel6))
                .add(0, 32, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(0, 89, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(81, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

 
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        jLabel7.setText(String.valueOf(jSlider1.getValue()));
        populateTable();
    }//GEN-LAST:event_jSlider1StateChanged

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        javax.swing.JComboBox cb = (javax.swing.JComboBox)evt.getSource();
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        String courseName = (String)cb.getSelectedItem();
        List<Course> appetizersList = dataModel.getAppetizers();
        Course appertizer = dataModel.getAppetizersByName(courseName);
        
        for (Course ap : appetizersList) {
            for (Ingredient ig : ap.getIngredientsList() ) {
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    if (tableModel.getValueAt(i, 0).equals(ig.getName())) {                       //dataModel.setTotalSum(dataModel.getTotalSum() - (ig.getCost() * jSlider1.getValue()));
                        dataModel.removeIngredientFromListByName(ig.getName());
                    }
                }
            }
        }
        if (!courseName.equalsIgnoreCase("Select...")) {
            for (Ingredient ingredient : appertizer.getIngredientsList()) {
                boolean addData = true;
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    int tblValue = (Integer)tableModel.getValueAt(i, 4);
                    if (tblValue == ingredient.getId()) {
                        addData = false;
                    }       
                }
                if (addData) {
                    dataModel.addIngredientToList(ingredient);
                }
            }    
        }
        populateTable();
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        userClickedCmb4 = false;
        javax.swing.JComboBox cb = (javax.swing.JComboBox)evt.getSource();
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        String courseName = (String)cb.getSelectedItem();
        List<Course> mainCoursesList = dataModel.getMainCourses();
        Course mainCourse = dataModel.getMainCoursesByName(courseName);
        
        for (Course mc : mainCoursesList) {
            for (Ingredient ig : mc.getIngredientsList() ) {
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    if (tableModel.getValueAt(i, 0).equals(ig.getName())) {
                        dataModel.removeIngredientFromListByName(ig.getName());
                    }
                }
            }
        }
        if (!courseName.equalsIgnoreCase("Select...")) {
            for (Ingredient ingredient : mainCourse.getIngredientsList()) {
                boolean addData = true;
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    int tblValueId = (Integer)tableModel.getValueAt(i, 4);
                    if (tblValueId == ingredient.getId()) {
                        addData = false;
                    }
                }
                if (addData) {
                    dataModel.addIngredientToList(ingredient);
                }
            }
        }
        if (mainCourse != null) {
            if (mainCourse.getSideCoursesList() != null) {
                if (mainCourse.getSideCoursesList().size() > 0) {
                    jComboBox4.removeAllItems();
                    jComboBox4.addItem("Select...");
                    jComboBox4.setEnabled(true);
                    for (Course sideCourse : mainCourse.getSideCoursesList()) {

                        jComboBox4.addItem(sideCourse.getName());
                    }
                }
            }
            else {
                jComboBox4.removeAllItems();
                jComboBox4.addItem("Select...");
                jComboBox4.setEnabled(false);
            }
        }
        else {
            jComboBox4.removeAllItems();
            jComboBox4.addItem("Select...");
            jComboBox4.setEnabled(false);
        }
        
        populateTable();
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
        javax.swing.JComboBox cb = (javax.swing.JComboBox)evt.getSource();
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        String courseName = (String)cb.getSelectedItem();
        List<Course> desertList = dataModel.getDesserts();
        Course dessert = dataModel.getDessertsByName(courseName);
        
        for (Course ds : desertList) {
            for (Ingredient ig : ds.getIngredientsList() ) {
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    if (tableModel.getValueAt(i, 4).equals(ig.getId())) {
                       dataModel.removeIngredientFromListByName(ig.getName());
                    }
                }
            }
        }
        if (!courseName.equalsIgnoreCase("Select...")) {
            for (Ingredient ingredient : dessert.getIngredientsList()) {
                boolean addData = true;
                for (int i = 0; i < tableModel.getRowCount(); i++) {
                    int tblValue = (Integer)tableModel.getValueAt(i, 4);
                    if (tblValue == ingredient.getId()) {
                        addData = false;
                    }       
                }
                if (addData) {
                    dataModel.addIngredientToList(ingredient);
                }
            }
        }
        populateTable();
    }//GEN-LAST:event_jComboBox3ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jComboBox4.setSelectedIndex(0);
        jComboBox1.setSelectedIndex(0);
        jComboBox2.setSelectedIndex(0);
        jComboBox3.setSelectedIndex(0);
        jSlider1.setValue(1);
        dataModel.resetData();
        populateTable();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed
        javax.swing.JComboBox cb = (javax.swing.JComboBox)evt.getSource();
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        String courseName = (String)cb.getSelectedItem();
        
        if (courseName != null) {
//            if (!courseName.equalsIgnoreCase("Select...")) {
                List<Course> mainCoursesList = dataModel.getMainCourses();
                Course sideCourse = dataModel.getSideCourseByName(courseName);


                for (Course mc : mainCoursesList) {
                    if (mc.getSideCoursesList() != null) {
                        for (Course sc : mc.getSideCoursesList()) {
                            for (Ingredient ig : sc.getIngredientsList() ) {
                                for (int i = 0; i < tableModel.getRowCount(); i++) {
                                    if (tableModel.getValueAt(i, 0).equals(ig.getName())) {
                                        dataModel.removeIngredientFromListByName(ig.getName());
                                    }
                                }
                            }
                        }
                    }
       //         }
                if (!courseName.equalsIgnoreCase("Select...")) {
                    for (Ingredient ingredient : sideCourse.getIngredientsList()) {
                        boolean addData = true;
                        for (int i = 0; i < tableModel.getRowCount(); i++) {
                            int tblValueId = (Integer)tableModel.getValueAt(i, 4);
                            if (tblValueId == ingredient.getId()) {
                                addData = false;
                            }
                        }
                        if (addData) {
                            dataModel.addIngredientToList(ingredient);
                        }
                    }
                }
                populateTable();
            }
            
        }
    }//GEN-LAST:event_jComboBox4ActionPerformed

    private void jComboBox4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox4MouseClicked
        userClickedCmb4 = true;
    }//GEN-LAST:event_jComboBox4MouseClicked

    private void jComboBox4ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox4ItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox4ItemStateChanged

    private void jTable1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTable1PropertyChange

    }//GEN-LAST:event_jTable1PropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FoodplannerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FoodplannerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FoodplannerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FoodplannerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FoodplannerUI().setVisible(true);
            }
        });
    }
    
    void populateTable(){
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        clearTable();
        for (Ingredient ingredient : dataModel.getIngredientsList()){
            tableModel.addRow(new Object[]{ingredient.getName(), ingredient.getMeasureAndAmount((double)(jSlider1.getValue())), ingredient.isPurchased(), (ingredient.getCost() * jSlider1.getValue()), ingredient.getId()});
        }
        Double totalSum = dataModel.getTotalSum()*(double)jSlider1.getValue();
        jTextField1.setText(totalSum.toString());
    }
    
    
    void clearTable() {
        javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
        
        
        while ( tableModel.getRowCount() > 0 ) {           
//            tableModel.setValueAt(false, 0, 2);  
            tableModel.removeRow(0);
        }
    }
    
     public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        if (column != -1) {
            javax.swing.table.DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel();
            Object data = tableModel.getValueAt(row, column);
            int tblValueId = (Integer)tableModel.getValueAt(row, 4);

            if (data.equals(Boolean.TRUE)) {
                for (Ingredient ingredient : dataModel.getIngredientsList()) {
                    if (tblValueId == ingredient.getId()) {
                        ingredient.setPurchased(true);
                    }
                }
            }
            if (data.equals(Boolean.FALSE)) {
                for (Ingredient ingredient : dataModel.getIngredientsList()) {
                    if (tblValueId == ingredient.getId()) {
                        ingredient.setPurchased(false);
                    }
                }
            }
            populateTable();
        }     
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
